﻿#include "DialogCalendar.h"
#include <QTGui>
#include <QTCore>

DialogCalendar::DialogCalendar(QWidget * parent, Qt::WFlags f): QDialog(parent, f)
{
	m_date = new QDate(QDate::currentDate());
	m_vl = new QVBoxLayout(this);
	m_hl = new QHBoxLayout();
	m_grid = new QGridLayout();
	m_vl->addLayout(m_hl);
	m_vl->addLayout(m_grid);

	createYearAndMonth();
	createHeader();
	createButtonGroup();
	createButtons();
	m_vl->addStretch();
	refresh(m_date);
	setFixedSize(QSize(270,240));
}

DialogCalendar::~DialogCalendar(void)
{
}

void DialogCalendar::daySelected(int i)
{
	m_date->setYMD(m_date->year(), m_date->month(), i + 1);
}

void DialogCalendar::monthSelected(int i)
{

	m_date->setYMD(m_date->year(), i + 1, m_date->day());
	refresh(m_date);
}

void DialogCalendar::yearSelected(int i)
{
	m_date->setYMD(i, m_date->month() , m_date->day());
	refresh(m_date);
}

void DialogCalendar::refresh(QDate* date)
{
	int day = date->day();
	int daysInMonth = date->daysInMonth();
	int dayOfWeek = date->dayOfWeek();
	int dayOfFirst = (date->addDays(1 - day )).dayOfWeek();

	int pos = 0;
	int count = 0;

	for (int n = 0; n < m_buttonGroup->buttons().size(); n++)
	{
		m_grid->removeWidget(m_buttonGroup->button(n));
		m_buttonGroup->button(n)->setVisible(false);
	}

	for (int i = 1; i < 7; i++)
	{
		for (int j = 0; j < 7; j++)
		{
			if(count >= dayOfFirst)
			{
				if(pos < daysInMonth)
				{	
					m_grid->addWidget(m_buttonGroup->button(pos), i, j);
					m_buttonGroup->button(pos)->setVisible(true);
					pos++;
				}
			}
			count++;
		}
	}
	m_buttonGroup->button(day -1)->setChecked(true);
}

void DialogCalendar::createButtonGroup(void)
{
	m_buttonGroup = new QButtonGroup(this);

	for (int i = 0; i < 31; i++)
	{
		QPushButton *but = new QPushButton(tr("%1").arg(i + 1),this);
		but->setMaximumWidth(32);
		{
			QPalette p = but->palette();
			p.setColor( QPalette::Active, QPalette::Dark, QColor::fromRgb(180, 180, 255) );
			p.setColor( QPalette::Active, QPalette::Light, QColor::fromRgb(180, 180, 255) );
			p.setColor( QPalette::Active, QPalette::Button,QColor::fromRgb(180, 180, 255) );
			p.setColor( QPalette::Active, QPalette::ButtonText, Qt::black );
			but->setPalette(p);
		}
		but->setCheckable(true);
		but->setVisible(false);
		m_buttonGroup->addButton(but, i);
	}

	connect( m_buttonGroup,
			 SIGNAL( buttonClicked( int ) ), 
			 this, 
			 SLOT( daySelected( int ) ) ) ;
}

void DialogCalendar::createHeader(void)
{
	QStringList list;
	list << tr("Sun")
		 << tr("Mon")
		 << tr("Tue")
		 << tr("Web")
		 << tr("Thu")
		 << tr("Fri")
		 << tr("Sat");

	for(int i = 0; i < 7; i++)
	{
		QPushButton *but = new QPushButton(this);

		{
			QPalette p = but->palette();
			p.setColor( QPalette::Active, QPalette::Dark, Qt::darkGray );
			p.setColor( QPalette::Active, QPalette::Light, Qt::darkGray);
			p.setColor( QPalette::Active, QPalette::Button,Qt::darkGray );
			p.setColor( QPalette::Active, QPalette::ButtonText, Qt::white );
			but->setPalette(p);
		}
		but->setText(list[i]);
		but->setMaximumWidth(32);
		m_grid->addWidget(but, 0, i);
	}
}

void DialogCalendar::createYearAndMonth(void)
{
	QComboBox *com = new QComboBox(this);
	com->setMinimumWidth(128);
	QStringList list;
	list<< tr("Jan") 
		<< tr("Feb") 
		<< tr("Mar") 
		<< tr("Apr") 
		<< tr("May")
		<< tr("Jun")
		<< tr("Jun")
		<< tr("Aug")
		<< tr("Sep")
		<< tr("Oct")
		<< tr("Nov")
		<< tr("Dec");

	com->addItems(list);
	com->setCurrentIndex(m_date->month() - 1);
	connect( com,
			 SIGNAL( activated( int ) ), 
			 this, 
			 SLOT( monthSelected( int ) ) ) ;

	QSpinBox *spin = new QSpinBox(this);
	spin->setRange(0, 3000);
	spin->setMinimumWidth(128);
	spin->setValue(m_date->year());
	connect( spin,
			 SIGNAL( valueChanged( int ) ), 
			 this, 
			 SLOT( yearSelected( int ) ) ) ;

	m_hl->addWidget(com);
	m_hl->addWidget(spin);
	m_hl->addStretch();
	setWindowTitle(tr("Please Selete Date"));
}

void DialogCalendar::createButtons(void)
{
	QHBoxLayout *h2 = new QHBoxLayout();
	m_vl->addLayout(h2);
	QPushButton *bOk = new QPushButton(tr("OK"),this);
	connect( bOk,
			 SIGNAL( clicked() ), 
			 this, 
			 SLOT( accept() ) ) ;
	bOk->setMaximumWidth(80);
	QPushButton *bNo = new QPushButton(tr("Cancel"),this);
	connect( bNo,
			 SIGNAL( clicked() ), 
			 this, 
			 SLOT( reject() ) ) ;
	bNo->setMaximumWidth(80);
	h2->addWidget(bOk);
	h2->addWidget(bNo);
}

const QDate& DialogCalendar::date(void)
{
	return *m_date;
}
